using System;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node pointer in the grammar(AnsiC).
    /// </summary>
    internal partial class Vnpointer : IFullFormat {
        // [138] pointer = '*' ;
        // [139] pointer = '*' type_qualifier_list ;
        // [140] pointer = '*' pointer ;
        // [141] pointer = '*' type_qualifier_list pointer ;


        private readonly List<IFullFormat> prefix = new();
        private readonly IFullFormat complex;

        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;
        public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
            var config = new BlankConfig();
            var prefixCount = this.prefix.Count;
            for (var i = 0; i < prefixCount; i++) {
                context.PrintBlanksAnd(this.prefix[i], i == 0 ? preConfig : config, writer);
                if (i + 1 < prefixCount) {
                    context.PrintCommentsBetween(this.prefix[i], this.prefix[i + 1], config, writer);
                }
                else {
                    context.PrintCommentsBetween(this.prefix[i], this.complex, config, writer);
                }
            }
            context.PrintBlanksAnd(this.complex, prefixCount == 0 ? preConfig : config, writer);
        }

        // [140] pointer = '*' pointer ;
        public void Insert(Token r0) {
            this.prefix.Insert(0, new Prefix0(r0));
            this._scope.start = r0.index;
        }
        class Prefix0 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Token r0;
            public Prefix0(Token r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }

        // [141] pointer = '*' type_qualifier_list pointer ;
        public void Insert(Token r1, Vntype_qualifier_list r0) {
            this.prefix.Insert(0, new Prefix1(r1, r0));
            this._scope.start = r1.Scope.start;
        }
        class Prefix1 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                var config = new BlankConfig();
                context.PrintBlanksAnd(r1, preConfig, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
            public readonly Token r1;
            public readonly Vntype_qualifier_list r0;
            public Prefix1(Token r1, Vntype_qualifier_list r0) {
                this.r1 = r1;
                this.r0 = r0;
                this._scope = new TokenRange(r1, r0);
            }
        }


        // [138] pointer = '*' ;
        public Vnpointer(Token r0) {
            this.complex = new complex0(r0);
            this._scope = new TokenRange(r0);
        }
        class complex0 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Token r0;
            public complex0(Token r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }

        // [139] pointer = '*' type_qualifier_list ;
        public Vnpointer(Token r1, Vntype_qualifier_list r0) {
            this.complex = new complex1(r1, r0);
            this._scope = new TokenRange(r1, r0);
        }
        class complex1 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                var config = new BlankConfig();
                context.PrintBlanksAnd(r1, preConfig, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
            public readonly Token r1;
            public readonly Vntype_qualifier_list r0;
            public complex1(Token r1, Vntype_qualifier_list r0) {
                this.r1 = r1;
                this.r0 = r0;
                this._scope = new TokenRange(r1, r0);
            }
        }



    }
}
